package com.haotian.xml;

import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.naming.NoNameCoder;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;

public class xsTest {
	public static void main(String[] args) {

		// Bean转XML

		// 初始化TestBean 对象
		TestBean bean = new TestBean();

		bean.setName("TestPDFText");
		bean.setAge(18);

		List<TestBean2> list = new ArrayList<>();

		TestBean2 bean1 = new TestBean2();
		bean1.setName("TestPDFText");
		bean1.setAge(18);

		TestBean2 bean2 = new TestBean2();
		bean2.setName("TestPDFText");
		bean2.setAge(18);
		list.add(bean1);
		list.add(bean2);

		// list 压入对象
		bean.setBeans(list);

		// 创建xstream对象
		XStream xstream = new XStream(new XppDriver(new NoNameCoder()) {
			@Override
			public HierarchicalStreamWriter createWriter(Writer out) {
				return new PrettyPrintWriter(out) {

					@Override
					@SuppressWarnings("rawtypes")
					public void startNode(String name, Class clazz) {
						super.startNode(name, clazz);
					}

					@Override
					public String encodeNode(String name) {
						return name;
					}

					@Override
					protected void writeText(QuickWriter writer, String text) {
							writer.write("<![CDATA[");
							writer.write(text);
							writer.write("]]>");
					}
				};
			}
		});

		// 自动侦查注解
		xstream.autodetectAnnotations(true);

		// 设置节点名称 绑定到指定的类 输出xml 的时候 就不会显示的 显示包了
		// xstream.alias("TestBean", TestBean.class);
		xstream.alias("TestBean2", TestBean2.class);
		String xml = null;
		System.out.println(xml = xstream.toXML(bean));

		TestBean fromBean = (TestBean) xstream.fromXML(xml);
		System.out.println(fromBean.getAge());
		System.out.println(fromBean.getName());
		System.out.println(fromBean.getBeans().get(0).getAge());
		System.out.println(fromBean.getBeans().get(0).getName());
		System.out.println(fromBean.getBeans().get(1).getAge());
		System.out.println(fromBean.getBeans().get(1).getName());
		
		
		
		
		//list 结构的 TestBean2
		
		
		List<TestBean2> list2 = new ArrayList<>();
		
		TestBean2 b2= new TestBean2();
		b2.setAge(1);
		b2.setName("asdasd");
		list2.add(b2);
		TestBean2 b21= new TestBean2();
		b21.setAge(2);
		b21.setName("asdasd2");
		list2.add(b21);
		TestBean2 b22= new TestBean2();
		b22.setAge(3);
		b22.setName("asdasd3");
		list2.add(b22);
		
		xstream.alias("beans", java.util.List.class);
		String list2xml = xstream.toXML(list2);
		
		System.out.println(list2xml);
		
		
		
		
		//xml 转 list 
		
		@SuppressWarnings("unchecked")
		List<TestBean2> fromLists =  (List<TestBean2>) xstream.fromXML(list2xml);
		
		for (TestBean2 testBean2 : fromLists) {
			System.out.println(testBean2.getAge());
			System.out.println(testBean2.getName());
		}
		
		
		
		//给XStream开启CDATA
		
	}
}
